<!-- src/views/RentalStep2.vue -->
<template>
  <div class="rental-form">
    <!-- 顶部导航 -->
    <div class="nav-header">
      <div class="back">
        <i class="el-icon-arrow-left" @click="goBack"></i>
        <span>我要转租</span>
      </div>
      <div class="icons">
        <el-button type="text" icon="el-icon-more"></el-button>
        <el-button type="text" icon="el-icon-s-help"></el-button>
      </div>
    </div>

    <!-- 进度指示器 -->
    <div class="step-indicator">
      <div class="step completed">1</div>
      <div class="line"></div>
      <div class="step active">2</div>
      <div class="line"></div>
      <div class="step">3</div>
    </div>

    <!-- 表单内容 -->
    <div class="form-content">
      <!-- 环境要求 -->
      <div class="form-item">
        <div class="form-title">房子的居住情况与优点，希望你能喜欢</div>
        <el-input
            type="textarea"
            v-model="formData.introduce"
            placeholder="房子的装修情况，周边商场及主要功能区，到期后是否可以续租等"
            :rows="3"
        ></el-input>
      </div>
      <!-- 租金信息 -->
      <div class="form-item">
        <div class="form-title">我的月租预算、租期和租赁方式是这样的</div>
        <div class="select-group">
          <el-input v-model.number="formData.rent" placeholder="租金">
            <template slot="append">元/月</template>
          </el-input>
          <el-select v-model="formData.tenancyTerm" placeholder="3个月">
            <el-option label="3个月" :value="3"></el-option>
            <el-option label="6个月" :value="6"></el-option>
            <el-option label="12个月" :value="12"></el-option>
            <el-option label="两年" :value="24"></el-option>
            <el-option label="其他" :value="0"></el-option>
          </el-select>
          <el-select v-model="formData.payType" placeholder="押一付一">
            <el-option label="押一付一" value="押一付一"></el-option>
            <el-option label="押一付三" value="押一付三"></el-option>
            <el-option label="押一付六" value="押一付六"></el-option>
            <el-option label="押一付十二" value="押一付十二"></el-option>
          </el-select>
        </div>
      </div>
      <!-- 环境要求 -->
      <div class="form-item">
        <div class="form-title">我希望来租房的你是这样的</div>
        <el-input
            type="textarea"
            v-model="formData.renterRequire"
            placeholder="比如爱干净、不养宠物"
            :rows="3"
        ></el-input>
      </div>
      <!-- 个人介绍 -->
      <div class="form-item">
        <div class="form-title">简单介绍下我的情况，你会相信我说的话</div>
        <el-input
            type="textarea"
            v-model="formData.landlord"
            placeholder="比如上班族，性别，年龄..."
            :rows="3"
        ></el-input>
      </div>
      <!-- 有效期 -->
      <div class="form-item">
        <div class="form-title">房子合约的到期时间时</div>
        <el-date-picker
            v-model="formData.contractExpiration"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width: 100%">
        </el-date-picker>
      </div>
      <!-- 入住时间 -->
      <div class="form-item">
        <div class="form-title">房子的可入住时间</div>
        <el-date-picker
            v-model="formData.moveTime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width: 100%">
        </el-date-picker>
      </div>
      <!-- 按钮组 -->
      <div class="button-group">
        <el-button @click="handlePrev">上一步</el-button>
        <el-button @click="handleSubmit">下一步</el-button>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: 'SubletStep2',
  data() {
    return {
      formData: {
        introduce: '',
        rent: null,
        tenancyTerm: 3,
        payType: '押一付一',
        renterRequire: '',
        landlord:'',
        contractExpiration: '',
        moveTime: ''
      },
      submitting: false
    }
  },
  created() {
    if (this.$store && this.$store.state.formData) {
      const savedData = this.$store.state.formData.sublet2
      if (savedData) {
        this.formData = { ...savedData }
      }
    }
  },
  methods: {
    handlePrev() {
      try {
        if (this.$store) {
          this.$store.commit('SET_SUBLET2_DATA', this.formData)
        }
        this.$router.push('/subletStep1')
      } catch (error) {
        console.error('Error in handlePrev:', error)
        this.$message.error('返回上一步失败')
      }
    },
    async handleSubmit() {
      if (!this.formData.introduce) {
        this.$message.error('请输入房子的居住情况与优点')
        return
      }
      if (!this.formData.rent) {
        this.$message.error('请输入租金')
        return
      }
      if (!this.formData.renterRequire) {
        this.$message.error('请输入租客要求')
        return
      }
      if (!this.formData.landlord) {
        this.$message.error('请输入房东介绍')
        return
      }
      if (!this.formData.contractExpiration) {
        this.$message.error('请选择合同到期时间')
        return
      }
      if (!this.formData.moveTime) {
        this.$message.error('请选择可入住时间')
        return
      }

      try {
        // 保存数据到 Vuex
        if (this.$store) {
          this.$store.commit('SET_SUBLET2_DATA', this.formData)
        }
        // 跳转到第二步
        this.$router.push('/subletStep3')
      } catch (error) {
        console.error('Error in handleNext:', error)
      }
    },
    goBack(){
      this.$router.push('/release')
    }
  }
}
</script>


<style scoped>
.icons {
  display: flex;
  border: 1px solid white;
  padding: 5px 5px;
  border-radius: 15px;
}

.icons button {
  color: white;
  padding: 0 5px;
  font-size: 18px;
}

.rent-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.rental-form {
  max-width: 414px;
  margin: 0 auto;
  background: #f5f5f5;
}

.nav-header {
  background: #ff7f50;
  color: white;
  padding: 10px 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.step-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 0;
}
.el-button.is-loading {
  pointer-events: none;
}

.step {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ddd;
}

.step.active {
  background: #409EFF;
  color: white;
}

.step.completed {
  background: #67C23A;
  color: white;
}

.line {
  width: 100px;
  height: 1px;
  background: #ddd;
  margin: 0 10px;
}

.line.completed {
  background: #67C23A;
}

.form-content {
  padding: 15px;
}

.form-item {
  background: white;
  padding: 15px;
  margin-bottom: 10px;
  border-radius: 8px;
}

.form-title {
  margin-bottom: 10px;
  color: #333;
}

.button-group {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

:deep(.el-button) {
  width: 45%;
}
.select-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
</style>
